<?php

namespace app\index\traits;

use app\common\model\AccountBill;
use app\common\model\AccountUser;
use app\common\model\UserPersonnel;
use ShidianUtil\Utils\StringUtil;
use ShidianUtil\Utils\TimeUtil;

/**
 * 公司代付
 * Trait PaymentProxyTrait
 * @package app\index\traits
 */
trait PaymentProxyTrait
{


    /**
     * 检查是否可以进行代付（存在代付账号，剩余配额大于0）
     * @param int $memberId
     * @param int $companyId
     * @param int $userPersonnelId
     * @param string $payMethod
     * @return bool
     */
    private function checkProxyQuota($memberId, $companyId, $userPersonnelId, $payMethod): bool
    {

        $account = AccountUser::where('id', $companyId)->where('payMethod', $payMethod)->find();

        if (!$account || $account->balance <= 0) {
            return false;
        }

        $userPersonnel = UserPersonnel::where('id', $userPersonnelId)->find();
        if (!$userPersonnel) {
            return false;
        }

        $quota = 0;
        if (StringUtil::equals($payMethod, PAYMENT_CARD)) {
            $quota = $userPersonnel->quotaCard;
        }
        if (StringUtil::equals($payMethod, PAYMENT_COIN)) {
            $quota = $userPersonnel->quotaCoin;
        }

        // 直接超出限额
        if ($quota <= 0) {
            return false;
        }

        // 已使用金额
        $used = AccountBill::where('fromMemberId', $companyId)
            ->where('fromType', 'company')
            ->where('proxyMemberId', $memberId)
            ->where('proxyUserPersonnelId', $userPersonnelId)
            ->where('accountType', $payMethod)
            ->where('created', '>=', TimeUtil::stamp(TimeUtil::DAY))
            ->sum('money');

        $used = is_null($used) ? 0 : $used;

        // 剩余配额
        $surplus = $quota - $used;

        return $surplus > 0;
    }

    /**
     * 检查公司代付剩余配额是否足够支付
     * @param int $memberId
     * @param int $companyId
     * @param int $userPersonnelId
     * @param string $payMethod
     * @param float $amount
     * @return bool
     */
    private function checkProxyProxyAndBalance($memberId, $companyId, $userPersonnelId, $payMethod, $amount):bool{


        $account = AccountUser::where('id', $companyId)->where('payMethod', $payMethod)->find();

        if (!$account || $account->balance <= 0) {
            return false;
        }

        $userPersonnel = UserPersonnel::where('id', $userPersonnelId)->find();
        if (!$userPersonnel) {
            return false;
        }

        $quota = 0;
        if (StringUtil::equals($payMethod, PAYMENT_CARD)) {
            $quota = $userPersonnel->quotaCard;
        }
        if (StringUtil::equals($payMethod, PAYMENT_COIN)) {
            $quota = $userPersonnel->quotaCoin;
        }

        // 直接超出限额
        if ($quota <= 0 || $quota < $amount) {
            return false;
        }

        // 已使用金额
        $used = AccountBill::where('fromMemberId', $companyId)
            ->where('fromType', 'company')
            ->where('proxyMemberId', $memberId)
            ->where('proxyUserPersonnelId', $userPersonnelId)
            ->where('accountType', $payMethod)
            ->where('created', '>=', TimeUtil::stamp(TimeUtil::DAY))
            ->sum('money');

        $used = is_null($used) ? 0 : $used;

        // 剩余配额
        $surplus = $quota - $used - $amount;

        return $surplus >= 0;

    }

}